﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Carbon.Web.Forms
{
    public static class FormsUtility
    {
        public static void ExecuteStartupScript(Control control, String script)
        {
            Page page = control.Page;
            if (page == null) throw new NullReferenceException("page");

            ScriptManager sm = ScriptManager.GetCurrent(page);

            if (sm != null && sm.IsInAsyncPostBack)
            {
                ScriptManager.RegisterStartupScript(control.Page, control.GetType(), script, string.Format(@"<script type=""text/javascript"">{0}</script>", script), false);
            }
            else
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), script, string.Format(
@"<script type=""text/javascript"">
(function() {{
var onload = function() {{
    {0};
    Sys.Application.remove_load(onload);
}};

Sys.Application.add_load(onload);
}})();
</script>", script), false);
            }
        }
    }
}
